<?php
class News extends CI_Controller {

	
     function __construct() 
     {
     	
        parent::__construct();
        $this->load->database();
        $this->lang->load('admin_news');
        $this->load->model('news_db');
        $this->load->helper('form');
        $this->load->helper('language');
		$this->load->helper('url');
        $this->load->model('category_db');
        if($this->session->userdata('level') < 3 )
        {
        	redirect('http://google.be','refresh');
        }
     }
     
     function index()
     {
     	$data['page_title'] = 'Ajouter une news';
		$data['folder'] = 'news';
		$data['main_content'] = 'view.php';
		$data['query'] = $this->news_db->getAll('10');
		$this->load->view('admin/index',$data);
     }
     
     
     function add($action = NULL)
     {
     	if($action!='add_news')
     	{
	     	$data['page_title'] = 'Ajouter une news';
			$data['folder'] = 'news';
			$data['category'] = $this->category_db->getCategory_list();
			$data['main_content'] = 'add.php';
			$this->load->view('admin/index',$data);
     	}
     	else 
     	{
     		//Validation des champs du formulaire 
     		$this->load->library('form_validation');
			$this->form_validation->set_rules('title', 'titre', 'required|min_length[5]|max_length[100]');
			$this->form_validation->set_rules('content', 'contenu', 'required|min_length[100]');
     		if ($this->form_validation->run() == FALSE)
				{
					$data['page_title'] = 'Ajouter une news';
					$data['folder'] = 'news';
					$data['category'] = $this->category_db->getCategory_list();
					$data['main_content'] = 'add.php';
					$this->load->view('admin/index',$data);
				}
			else
				{
		     		$title= $_POST['title'];
		     		$content = $_POST['content'];
		     		$author = $this->session->userdata('username');
		     		$id_category=$_POST['category'];
		     		$this->news_db->addNews($content,$title,$author,$id_category);
		     		$data['statut'] = "ajoutée";
			     	$data['folder'] = 'news';
					$data['main_content'] = 'redirect.php';
					$this->load->view('admin/index',$data);
				}
     	}
     }
     
     function del($news_id)
     {
     	if($this->news_db->deletteNews($news_id)==TRUE)
     	{
	     	//redirect('/admin/news','refresh');
	     	$data['statut'] = "supprimée";
	     	$data['folder'] = 'news';
			$data['main_content'] = 'redirect.php';
			$this->load->view('admin/index',$data);
     	}
     	else 
     	{
     		//ERREUR
     	}
     	
     }
     
     function edit($news_id)
     {
     	$data['news'] = $this->news_db->getNews($news_id);
     	$data['page_title'] = 'Editer une news';
		$data['folder'] = 'news';
		$data['category'] = $this->category_db->getCategory_list();
		$data['main_content'] = 'edit.php';
		$this->load->view('admin/index',$data);
     }
     
}